/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.dbcommon;

import javax.naming.*;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.*;

public class DBCPoolingListener implements ServletContextListener{
	/**
	 * Servlet server will use this method to init data base connection pool.
	 * @param sce It is ServletContextEvent type. It is sended by servlet server.
	 */
	public void contextInitialized(ServletContextEvent sce) {
		try {
			//Obtain our environment naming context
			Context envCtx = (Context) new InitialContext().lookup("java:comp/env");

			//Look up our data source
			DataSource ds = (DataSource) envCtx.lookup("jdbc/gtdb");
			sce.getServletContext().setAttribute("DBCPool", ds);
		} catch(NamingException e) {
			e.printStackTrace();
		}
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}
}
